﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using YJKRevitCode.ElmentSearchUtility.LinkDocumentSuite;
using ModelCheckUtility.ModelLayer;

namespace ModelCheckUtility.ControlLayer
{
	internal class BaseFamilyInstanceFinderByCurve
	{
		public BaseFamilyInstanceFinderByCurve(Document inputDoc, List<ResultBeanWithGeometryPacker> lstUseElementGeoBean = null)
		{
			this.m_lstPackerBean = new List<BaseFamilyInstanceBoundaryPacker>();
			this.m_doc = inputDoc;
			this.m_lstAllLevels = Utility.GetAllLevels(this.m_doc);
			List<ResultBeanWithGeometryPacker> list;
			if (lstUseElementGeoBean == null)
			{
				list = LinkDocumentUtility.QuickGetDocumetDoorBean(inputDoc);
			}
			else
			{
				list = lstUseElementGeoBean;
			}
			foreach (ResultBeanWithGeometryPacker resultBeanWithGeometryPacker in list)
			{
				FamilyInstance familyInstance = null;
				if (resultBeanWithGeometryPacker.UseElement is FamilyInstance)
				{
					familyInstance = (resultBeanWithGeometryPacker.UseElement as FamilyInstance);
				}
				if (familyInstance != null && resultBeanWithGeometryPacker.UseElementGeometryPacker != null && resultBeanWithGeometryPacker.UseElementGeometryPacker.IfCanUse)
				{
					BaseFamilyInstanceBoundaryPacker item = new BaseFamilyInstanceBoundaryPacker(familyInstance, this.m_lstAllLevels, resultBeanWithGeometryPacker.UseElementGeometryPacker, resultBeanWithGeometryPacker.UseLinkeInstance);
					this.m_lstPackerBean.Add(item);
				}
			}
		}

		public List<BaseDoorPacker> FindCurveLinkBaseDoorPacker(List<Curve> lstCurveInput)
		{
			List<BaseDoorPacker> list = new List<BaseDoorPacker>();
			foreach (Curve inputCurve in lstCurveInput)
			{
				list.AddRange(this.FindCurveLinkBaseDoorPacker(inputCurve));
			}
			list = list.Distinct<BaseDoorPacker>().ToList<BaseDoorPacker>();
			return list;
		}

		private List<BaseDoorPacker> FindCurveLinkBaseDoorPacker(Curve inputCurve)
		{
			List<BaseDoorPacker> list = new List<BaseDoorPacker>();
			foreach (BaseFamilyInstanceBoundaryPacker baseFamilyInstanceBoundaryPacker in this.m_lstPackerBean)
			{
				BaseElementPacker baseElementPacker;
				if (baseFamilyInstanceBoundaryPacker.IfCurveIntersectionThis(inputCurve) && baseFamilyInstanceBoundaryPacker.TryGetBaseElementPacker(out baseElementPacker) && baseElementPacker is BaseDoorPacker)
				{
					list.Add(baseElementPacker as BaseDoorPacker);
				}
			}
			return list;
		}

		private Document m_doc;

		private List<BaseFamilyInstanceBoundaryPacker> m_lstPackerBean;

		private List<Level> m_lstAllLevels = new List<Level>();
	}
}
